package com.chuantou.yuba.db.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.Thumbnails;
import android.support.v4.util.LongSparseArray;

import com.chuantou.yuba.pojo.Album;
import com.chuantou.yuba.pojo.AlbumBucket;

public class AlbumHelper {
	private LongSparseArray<String> mThumbnails = new LongSparseArray<String>();
	private LongSparseArray<AlbumBucket> mBuckets = new LongSparseArray<AlbumBucket>();
	private List<Album> mAllImages = new ArrayList<Album>();
	private Context mContext;

	public AlbumHelper(Context context) {
		this.mContext = context;
	}

	public String getThumbnail(long id) {
		return mThumbnails.get(id);
	}

	public List<AlbumBucket> getAllBuckets() {
		getThumbnails();
		// 构造相册索引
		String columns[] = new String[] { Media._ID, Media.BUCKET_ID, Media.DATA, Media.BUCKET_DISPLAY_NAME };
		// 得到一个游标
		Cursor cursor = mContext.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, null, null, Media.DATE_MODIFIED + " desc");
		if (cursor != null) {
			try {
				while (cursor.moveToNext()) {
					long imageId = CursorHelper.getLong(cursor, Media._ID, 0);
					String path = CursorHelper.getString(cursor, Media.DATA, null);
					String displayName = CursorHelper.getString(cursor, Media.BUCKET_DISPLAY_NAME, "");
					long bucketId = CursorHelper.getLong(cursor, Media.BUCKET_ID, 0);
					Album album = new Album();
					album.setId(imageId);
					album.setPath(path);
					album.setBucketId(bucketId);
					mAllImages.add(album);
					AlbumBucket bucket = mBuckets.get(bucketId);
					if (bucket == null) {
						bucket = new AlbumBucket();
						bucket.setId(bucketId);
						bucket.setName(displayName);
						mBuckets.put(bucketId, bucket);
					}
					bucket.setCount(bucket.getCount() + 1);
					bucket.addImage(album);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				cursor.close();
			}
		}
		List<AlbumBucket> buckets = new ArrayList<AlbumBucket>();
		int size = mBuckets.size();
		for (int i = 0; i < size; i++) {
			buckets.add(mBuckets.valueAt(i));
		}
		return buckets;
	}

	public List<Album> getLatestAlbums(int count) {
		if (mAllImages.size() < count) {
			return mAllImages;
		}
		List<Album> list = new ArrayList<Album>(count);
		for (int i = 0; i < count; i++) {
			list.add(mAllImages.get(i));
		}
		return list;
	}

	public void destroy() {
		mThumbnails.clear();
		mAllImages.clear();
		mBuckets.clear();
	}

	private void getThumbnails() {
		String[] projection = { Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails.DATA };
		Cursor cursor = Thumbnails.queryMiniThumbnails(mContext.getContentResolver(), Thumbnails.EXTERNAL_CONTENT_URI, Thumbnails.MINI_KIND, projection);
		if (cursor != null) {
			try {
				while (cursor.moveToNext()) {
					long id = CursorHelper.getLong(cursor, Thumbnails.IMAGE_ID, 0);
					String thumbnailPath = CursorHelper.getString(cursor, Thumbnails.DATA, null);
					mThumbnails.put(id, thumbnailPath);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				cursor.close();
			}
		}
	}
}
